<?php
/*
Plugin Name: VTP Employees Custom User Meta
Plugin URI: http://www.dudyland.sk
Description: VTP Employees extra data
Author: Peter Turza, Pavol Martiniak
Version: 0.1
Author URI: http://www.dudyland.sk

Add new custom user meta fields to the user's Profile in the WordPress Dashboard.

*/

class VTPEmployees {

    var $edited = 0;

    function __construct() {
        add_action('user_register', array(&$this, 'allow_all_categories_for_employee'), 10, 3);
        if ( is_admin() ) {
            // ak je v admin rozhrani
            // pridaj akcie
            add_action('show_user_profile', array(&$this,'action_show_user_profile'));
            add_action('edit_user_profile', array(&$this,'action_show_user_profile'));
            add_action('personal_options_update', array(&$this,'action_process_option_update'));
            add_action('edit_user_profile_update', array(&$this,'action_process_option_update'));
            // registruje skripty
            wp_register_script('jquery-validate', get_bloginfo(url).'/wp-includes/js/validate/jquery.validate.js');
        }
    }
    
    function allow_all_categories_for_employee($uid) {
        $userData = get_userdata($uid);
        if ($userData->wp_user_level >= 7) {
            UserCatsManager::save($uid, get_all_category_ids());
        }
    }

    function action_show_user_profile() {
        // nacita skripty
        wp_enqueue_script('jquery-validate');
        global $profileuser;
        // ktory user sa upravuje
        $user_id = $profileuser->ID;
        // ak je to editor (vtp zamestnanec)
        if (get_usermeta($user_id, 'user_level') == 7) {
            ?>
<h3><?php _e('VTP zamestnanec') ?></h3>

<table class="form-table">
    <tr>
        <th><label for="degree"><?php _e('Titul');?></label></th>
        <td><input type="text" name="degree" id="degreeMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'degree') );?>" /></td>
    </tr>
    <tr>
        <th><label for="position"><?php _e('Názov pozície');?></label></th>
        <td><input type="text" name="position" id="positionMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'position') );?>" class="regular-text" /></td>
    </tr>
    <tr>
        <th><label for="order"><?php _e('Poradie dôležitosti (1 je pre Jura :-), 2, 3 podľa uváženia, automaticky sa doplní 2)');?></label></th>
        <td><input type="text" name="order" id="orderMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'order') );?>" class="regular-text" /></td>
    </tr>
    <tr>
        <th><label for="icq"><?php _e('ICQ');?></label></th>
        <td><input type="text" name="icq" id="icqMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'icq') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="skype"><?php _e('Skype');?></label></th>
        <td><input type="text" name="skype" id="skypeMeta" value="<?php echo esc_attr(get_usermeta($user_id, 'skype') );?>" class="regular-text"/></td>
    </tr>
    <tr>
    <tr>
        <th><label for="phone1"><?php _e('Telefón 1');?></label></th>
        <td><input type="text" name="phone1" id="phone1Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone1') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="phone2"><?php _e('Telefón 2');?></label></th>
        <td><input type="text" name="phone2" id="phone2Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone2') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="phone3"><?php _e('Telefón 3');?></label></th>
        <td><input type="text" name="phone3" id="phone3Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'phone3') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="link1"><?php _e('Link 1');?></label></th>
        <td><input type="text" name="link1" id="link1Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'link1') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="link2"><?php _e('Link 2');?></label></th>
        <td><input type="text" name="link2" id="link2Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'link2') );?>" class="regular-text"/></td>
    </tr>
    <tr>
        <th><label for="link3"><?php _e('Link 3');?></label></th>
        <td><input type="text" name="link3" id="link3Meta" value="<?php echo esc_attr(get_usermeta($user_id, 'link3') );?>" class="regular-text"/></td>
    </tr>
</table>
<script type="text/javascript">
    var userPhotoValidation;
    if (document.getElementById("userphoto_delete") == null) {
        userPhotoValidation = {required: true, accept: "jpe?g|gif|png"};
    } else {
        userPhotoValidation = {accept: "jpe?g|gif|png"};
    }
    jQuery("#your-profile").validate({
        rules: {
            first_name: {required: true},
            last_name: {required: true},
            nickname: {required: true},
            email: {required: true, email: true},
            url: {url: true},
            order: {range: [0, 3]},
            userphoto_image_file: userPhotoValidation,
            position: {required: true},
            phone1: {},
            phone2: {},
            phone3: {},
            link1: {url: true},
            link2: {url: true},
            link3: {url: true}
        }
    });
</script>
            <?php
        }
    }

    //ulozi meta informacie
    function action_process_option_update() {
        // zisti ktoreho usera sme upravovali
        $user_id = $_POST['user_id'];
        update_usermeta($user_id, 'degree', ( isset($_POST['degree']) ? $_POST['degree'] : '' ) );
        update_usermeta($user_id, 'position', ( isset($_POST['position']) ? $_POST['position'] : '' ) );
        update_usermeta($user_id, 'order', ( !empty($_POST['order']) ? $_POST['order'] : 2) );
        update_usermeta($user_id, 'icq', ( isset($_POST['icq']) ? $_POST['icq'] : '' ) );
        update_usermeta($user_id, 'skype', ( isset($_POST['skype']) ? $_POST['skype'] : '' ) );
        update_usermeta($user_id, 'phone1', ( isset($_POST['phone1']) ? $_POST['phone1'] : '' ) );
        update_usermeta($user_id, 'phone2', ( isset($_POST['phone2']) ? $_POST['phone2'] : '' ) );
        update_usermeta($user_id, 'phone3', ( isset($_POST['phone3']) ? $_POST['phone3'] : '' ) );
        update_usermeta($user_id, 'link1', ( isset($_POST['link1']) ? $_POST['link1'] : '' ) );
        update_usermeta($user_id, 'link2', ( isset($_POST['link2']) ? $_POST['link2'] : '' ) );
        update_usermeta($user_id, 'link3', ( isset($_POST['link3']) ? $_POST['link3'] : '' ) );
    }

    function getEmployees() {
        global $wpdb;
        $userIds = $wpdb->get_col( $wpdb->prepare(
       "SELECT sel1.id id from
        (SELECT $wpdb->users.ID id FROM $wpdb->users 
            LEFT JOIN $wpdb->usermeta ON ($wpdb->usermeta.user_id = $wpdb->users.ID) 
            WHERE $wpdb->usermeta.meta_key = 'wp_user_level' 
            AND $wpdb->usermeta.meta_value = 7) as sel1
        LEFT JOIN $wpdb->usermeta ON ($wpdb->usermeta.user_id = sel1.id) 
        WHERE $wpdb->usermeta.meta_key = 'order'
        order by $wpdb->usermeta.meta_value asc"));
        $result = array();
        foreach ($userIds as $uid) {
            $result[] = get_userdata($uid);
        }
        return $result;
    }

    function getPhoto($uid, $thumbnail=true) {
        if (userphoto_exists($uid)) {
            if ($thumbnail) {
                echo userphoto__get_userphoto($uid, USERPHOTO_THUMBNAIL_SIZE, '', '', array(), '');
            } else {
                echo userphoto__get_userphoto($uid, USERPHOTO_FULL_SIZE, '', '', array(), '');
            }
        } else {
            echo '<img src="'.get_bloginfo(url).'/wp-content/plugins/nophoto.png" />';
        }
    }


}

/* Initialise outselves */
add_action('plugins_loaded', create_function('','global $vtpEmployees; $vtpEmployees = new VTPEmployees();'));
?>
